package application.controller;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.jsp.JspWriter;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

import application.models.Modwinewineries;

import library.Controller;

/**
 * Servlet implementation class winery
 */
public class winery extends Controller {
	private static final long serialVersionUID = 1L;
       	
	/**
	 * @see HttpServlet#list(HttpServletRequest request, HttpServletResponse response, JspWriter out)
	 */
	@SuppressWarnings("unchecked")	
	public void list(HttpServletRequest request, HttpServletResponse response, JspWriter out) throws ServletException, IOException{
		String name = this.getClass().getSimpleName();
		if(request.getSession().getAttribute("user")!= null) {
			//Prepare Session
			SessionFactory sessionFactory = new Configuration()
				.configure()
				.buildSessionFactory();
			//Start session
			Session session = sessionFactory.openSession();
			session.beginTransaction();
			
			List<Modwinewineries> winery = (List<Modwinewineries>) session.createQuery("from Modwinewineries").list();
			request.setAttribute("winery", winery);
			
			out.flush();
			include(request,response,"/layout/"+name+"/list.jsp");
			
			session.getTransaction().commit();
			session.close();
		}else {
			response.sendRedirect("administrator?op=login");
		}
	}
	
	@SuppressWarnings("unchecked")
	public void edit(HttpServletRequest request, HttpServletResponse response, JspWriter out) throws ServletException, IOException{
		String name = this.getClass().getSimpleName();
		int winery_id = Integer.parseInt((String) request.getParameter("id"));
		
		if(request.getSession().getAttribute("user")!= null) {
			//Prepare Session
			SessionFactory sessionFactory = new Configuration()
				.configure()
				.buildSessionFactory();
			//Start session
			Session session = sessionFactory.openSession();
			session.beginTransaction();
			
			Query sql = session.createQuery("from Modwinewineries where winery_id =:id");
			List<Modwinewineries> winerys = (List<Modwinewineries>) sql.setInteger("id", winery_id).list();
			if(winerys.isEmpty())
				response.sendRedirect(name+"?op=list");
			else {
				out.flush();
				request.setAttribute("winery", (Modwinewineries) winerys.get(0));
				include(request,response,"/layout/"+name+"/edit.jsp");
			}
			
			session.getTransaction().commit();
			session.close();			
		}else {
			response.sendRedirect("administrator?op=login");
		}
	}

	@SuppressWarnings({ "unchecked" })
	public void save(HttpServletRequest request, HttpServletResponse response, JspWriter out) throws ServletException, IOException{
		String name = this.getClass().getSimpleName();
		if(request.getSession().getAttribute("user")!= null) {
			//Prepare Session
			SessionFactory sessionFactory = new Configuration()
				.configure()
				.buildSessionFactory();
			//Start session
			Session session = sessionFactory.openSession();
			session.beginTransaction();			
			if(request.getParameter("id").isEmpty()){
				Modwinewineries winerys = new Modwinewineries();
				winerys.setWineryName(request.getParameter("winery"));
				session.save(winerys);
				response.sendRedirect(name+"?op=list");
			} else {
				int winery_id = Integer.parseInt((String) request.getParameter("id"));
				Query sql = session.createQuery("from Modwinewineries where winery_id =:id");
				
				List<Modwinewineries> winerys = (List<Modwinewineries>) sql.setInteger("id", winery_id).list();
				if(winerys.isEmpty()) {
					response.sendRedirect(name+"?op=list");
				} else {
					winerys.get(0).setWineryName(request.getParameter("winery"));
					session.save(winerys.get(0));	
					response.sendRedirect(name+"?op=list");
				}
			}
			
			session.getTransaction().commit();
			session.close();			
		}else {
			response.sendRedirect("administrator?op=login");
		}
	}
	
	public void add(HttpServletRequest request, HttpServletResponse response, JspWriter out) throws ServletException, IOException{
		String name = this.getClass().getSimpleName();
		if(request.getSession().getAttribute("user")!= null) {
			out.flush();
			request.setAttribute("winery", null);
			include(request,response,"/layout/"+name+"/edit.jsp");
		}else {
			response.sendRedirect("administrator?op=login");
		}
	}
	
	@SuppressWarnings("unchecked")
	public void delete(HttpServletRequest request, HttpServletResponse response, JspWriter out) throws ServletException, IOException {
		String name = this.getClass().getSimpleName();
		int winery_id = Integer.parseInt((String) request.getParameter("id"));
		if(request.getSession().getAttribute("user")!= null) {
			//Prepare Session
			SessionFactory sessionFactory = new Configuration()
				.configure()
				.buildSessionFactory();
			//Start session
			Session session = sessionFactory.openSession();
			session.beginTransaction();
			
			Query sql = session.createQuery("from Modwinewineries where winery_id =:id");
			List<Modwinewineries> winerys = (List<Modwinewineries>) sql.setInteger("id", winery_id).list();
			session.delete(winerys.get(0));
			
			session.getTransaction().commit();
			session.close();
			
			response.sendRedirect(name+"?op=list");
		} else {
			response.sendRedirect("administrator?op=login");
		}
	}
}
